

/**author:yebinx email:yebinx@21cm.com*/
export default class Utils{
  
    constructor(){

    }
    /**
     * 把other上的属性复制到tar上
    */
   public static copyObjAtt(tar,other){
       if(tar && other){
           for(let key in other){
               tar[key]=other[key];
           }
       }
   }

    /**
     * 把other上的属性复制到tar上 只复制string number，boolean类型
    */
   public static copyObjAtt2(tar,other){
        if(tar && other){
            let tvalue;
            for(let key in other){
                tvalue = other[key];
                if(typeof tvalue == "string" || typeof tvalue == "number" || typeof tvalue == "boolean")tar[key]=other[key];
            }
        }
   }

    /**
     * 把data1上的属性按照给定的带有指定字符_key的值复制到data2上
    */
   public static setDataByKey(data1,data2,_key){
        for(let key in data1){
            if(key.indexOf(_key)>=0)data2[key]=data1[key];
        }
    }


   public static isPassDay(year:number,mouth:number,day:number){
       let isPass=false;
       let tdate:Date = new Date();
       let time1:number = tdate.getTime();
       tdate.setFullYear(year);
       tdate.setMonth(mouth-1);
       tdate.setDate(day);
       let time2=tdate.getTime();
       if(time1>time2)isPass=true;
       return isPass;
   }

   
  public static getStrColorToRbg(_color:string){
      let tarr=[];
      for(let i=1;i<_color.length;i +=2){
          //console.log(_color.slice(i,i+2));
          tarr.push(parseInt("0x"+_color.slice(i,i+2))/255)
      }
      return tarr;
  }

   public static timeToNor(time:number,pre:string=":"):string{
        let h=0;
        if(time>-3600)h=Math.floor(time/3600);
        let m=0;
        if(time>=60)m=Math.floor((time%3600)/60);
        let s=time%60;
        let sh=h>=10?h+"":"0"+h;
        let sm=m>=10?m+"":"0"+m;
        let ss=s>=10?s+"":"0"+s;
        return  sh+pre+sm+pre+ss;
   }
   
   
   public static stringFormat(_str:string,_pamrm) {
		 for (var i = 0; i < _pamrm.length; i++) {
			 var re = new RegExp('\\{' + i + '\\}', 'gm');
			 _str = _str.replace(re, _pamrm[i]);
		 }
		 return _str;
    }
   
   /**获得arr1不在arr2里的元素*/
   public static getDifEle(_arr1,_arr2):Array<any>{
       let tarr=[];
       for(let i=0;i<_arr1.length;i++){
           let find=false;
            for(let j=0;j<_arr2.length;j++){
                if(_arr1[i]==_arr2[j]){
                    find=true;
                    break;
                }
            }
            if(!find)tarr.push(_arr1[i]);
       }
       return tarr;
   }

	/*灰图滤镜**/
   public static getGrayFilter(){
        var grayscaleMat = [0.3086, 0.6094, 0.0820, 0, 0, 0.3086, 0.6094, 0.0820, 0, 0, 0.3086, 0.6094, 0.0820, 0, 0, 0, 0, 0, 1, 0];
        //创建一个颜色滤镜对象，灰图
        var grayscaleFilter = new Laya.ColorFilter(grayscaleMat);
        return [grayscaleFilter];
    }

    /*黑图滤镜**/
    public static getBlackFliter(){
        var grayscaleMat = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0];
        var grayscaleFilter = new Laya.ColorFilter(grayscaleMat);
        return [grayscaleFilter];
    }

      /***创建一个颜色滤镜 r b g 255的值*/
      public static createColorFilter(_r:number=0,_g:number=0,_b:number=0){
        let redMat =
        [
            _r, 0, 0, 0, 0, //R
            0, _g, 0, 0, 0, //G
            0, 0, _b, 0, 0, //B
            0, 0, 0, 1, 0, //A
        ];

        //创建一个颜色滤镜对象,红色
        let filter = new Laya.ColorFilter(redMat);
        return [filter];
    }

    /**发光滤镜 _blur边缘模糊得大小 _offx：x轴方向偏移 _offy：y轴方向偏移*/
    public static createGlowFilter(_clor="#ffff00",_blur:number=10,_offx:number=0,_offy:number=0){
        // 创建一个发光滤镜
        const GlowFilter = Laya.GlowFilter;
        let glowFilter = new GlowFilter(_clor, _blur, _offx, _offy);
        // 设置滤镜集合为发光滤镜
        return [glowFilter];
    }

    public static removeSp(_sp:any){
        if(_sp && _sp.parent) _sp.parent.removeChild(_sp);
    }
	
	public static getNode(_node:any,_path:string){
		 let tnames = _path.split("/");
		let tnode=_node.getChildByName(tnames[0]);
		for(let i=1;i<tnames.length;i++){
		    tnode=tnode.getChildByName(tnames[i]);
		    if(!tnode)return null;
		}
		return tnode;
	}

    public static inIncludStr(_valuer:string,_strs:Array<string>){
        for(let i=0;i<_strs.length;i++){
            if(_valuer.indexOf(_strs[i])>=0)return true;
        }
        return false;
    }
	
	public static createRandomId(_count:number=5,_pre:string=""){
		let ttime = new Date().getTime();
		let tstr="";
		for(let i=0;i<_count;i++){
			tstr += Math.floor(Math.random()*10);
		}
		return _pre+tstr+ttime;
	}
	
	public static compareVersion(_v1:string,_v2:string){
		let tarr1 = _v1.split(".");
		let tarr2=_v2.split(".");
		let tlen1 = tarr1.length;
		let tlen2=tarr2.length;
		let tlen = Math.min(tlen1,tlen2);
		let tnum1:number;
		let tnum2:number;
		for(let i=0;i<tlen;i++){
			tnum1=parseInt(tarr1[i]);
			tnum2=parseInt(tarr2[i]);
			if(tnum1 != tnum2){
				if(tnum1>tnum2)return 1;
				else return -1;
			}
		}
		if(tlen1 != tlen2){
			if(tlen1>tlen2)return 1;
			else return -1;
		}
		return 0;
	}
}
